package com.jintoufs.weixin;

import com.jintoufs.domain.customer.Customer;
import com.jintoufs.service.customer.CustomerService;
import com.jintoufs.uuid.SnowflakeIdWorker;
import org.apache.commons.lang.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SupportAction {
    @Resource
    private CustomerService userService;

    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;
    /**
     * 获得String类型请求参数
     *
     * @param request
     * @param name
     * @return
     */
    public String getString(HttpServletRequest request, String name) {
        if (StringUtils.isNotEmpty(name)) {
            return request.getParameter(name);
        }
        return null;
    }

    /**
     * 获得Integer类型请求参数
     *
     * @param request
     * @param name
     * @return
     */
    public int getInteger(HttpServletRequest request, String name) {
        String value = getString(request, name);
        if (StringUtils.isNotEmpty(value)) {
            try {
                return Integer.parseInt(value);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }

        }
        return 0;
    }

    /**
     * 获得Double类型请求参数
     *
     * @param request
     * @param name
     * @return
     */
    public double getDouble(HttpServletRequest request, String name) {
        String value = getString(request, name);
        if (StringUtils.isNotEmpty(value)) {
            try {
                return Double.parseDouble(value);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }

        }
        return 0;
    }

    /**
     * 获得Boolean类型请求参数
     *
     * @param request
     * @param name
     * @return
     */
    public boolean getBoolean(HttpServletRequest request, String name) {
        String value = getString(request, name);
        if (StringUtils.isNotEmpty(value)) {
            try {
                return Boolean.getBoolean(value);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }

        }
        return false;
    }


    public String formatDate(Date date) {
        String dateStr = null;
        SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateStr = sdFormat.format(date);
        return dateStr;
    }

    public Date parseString(String time) throws ParseException {
        SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
        return sdFormat.parse(time);
    }

    public Customer getUserFromClient(String openId) {
        Map<String, Object> params = new HashMap<>();
        params.put("openId", openId);
        List<Customer> userList = userService.queryByParams(params);
        if (userList.isEmpty()) {
            return null;
        }
        return userList.get(0);
    }

    public boolean isUserValid(String keyStr, HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>();
        HttpSession session = request.getSession();
        String value = session.getAttribute(keyStr).toString();

        params.put("keyStr", keyStr);
        List<Customer> userList = userService.queryByParams(params);
        if (userList.isEmpty()) {
            return false;
        }
        return true;
    }

    public String getRandomCode() {
        SimpleDateFormat simpleDateFormat;
        simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String str = simpleDateFormat.format(date);
        Random random = new Random();
        int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数
        return rannum + str;// 当前时间
    }

    public Customer getCustomerByToken(String token){
        if (token==null){
            return null;
        }else {
            Map<String,Object> param = new HashMap<>();
            param.put("token",token);
            List<Customer> customers = userService.queryByParams(param);
            return customers==null||customers.size()==0?null:customers.get(0);
        }
    }

    public String getUUID(){

        return String.valueOf(snowflakeIdWorker.nextId());
    }
    public  String filterEmoji(String source) {
        if (source == null) {
            return source;
        }
        Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
        Matcher emojiMatcher = emoji.matcher(source);
        if (emojiMatcher.find()) {
            source = emojiMatcher.replaceAll("");
            return source;
        }
        return source;
    }

}
